<?php
class hschool_files{
	public $info_array = array();
	public $page_array = array();
	public $tree_array = array();

	static function &instance() {
		static $object;
		if(empty($object)) {
			$object = new self();
		}
		return $object;
	}

	/*
	*	取出一个文件信息
	*/
	public function get_one($id='') {
		$info = array();
		if($id){
			$sql = "SELECT * FROM ".DB::table('files')." WHERE file_id='".$id."' LIMIT 1";
			$info = DB::fetch_first($sql);	
		}
		return $info;
	}

	/*
	*	取出多个文件信息
	*/
	public function get_more($ids=array()) {
		$more = array();
		if(!empty($ids)){
			$sql = "SELECT * FROM ".DB::table('files')." WHERE file_id IN (".dimplode($ids).")";
			$more = DB::fetch_all($sql);	
		}
		return $more;
	}

	/*
	*	创建文件
	*/
	public function add($file_path,$file_name) {
		global $_G;
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
		if(empty($is_submit)){
			$grade_id = isset($_REQUEST['grade_id']) ? $_REQUEST['grade_id']:'';
			$subject_id = isset($_REQUEST['subject_id']) ? $_REQUEST['subject_id']:'';
			$file_path_array = explode('/',$file_path);
			$count_key = count($file_path_array)-1;
			$file_name_array = pathinfo($file_name);
			$data = array(
				'dept_id'=>$_G['dept_id'],
				'file_name'=>str_replace('.'.$file_name_array['extension'],'',$file_name),
				'file_extension'=>strtolower($file_name_array['extension']),
				'file_store_path'=>date('Ym').'/'.date('d'),//TODO 优化各种路径 随配置而变化 现在默认存储路劲是 4
				'file_real_name'=>str_replace('.'.$file_name_array['extension'],'',$file_path_array[$count_key]),
				'uid'=>$_G['user_id'],
				'grade_id'=>$grade_id,
				'subject_id'=>$subject_id,
				'file_size'=>filesize(SITE_ROOT.'./'.$file_path),
				'file_time' => TIMESTAMP
			);
			$insert_id = DB::insert('files',$data,1);
			return $insert_id;
		}else{
			$grade_array = $this->get_grade_array();
			$subject_array = $this->get_subject_array();
			$term_array = $this->get_term_array();
			include template('admin/hschool/files_add');	
		}
	}

	/*
	*	编辑文件
	*/
	public function edit() {
		global $_G;
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
		if(!empty($is_submit)){

		}else{
			$id = isset($_REQUEST['id']) ? $_REQUEST['id']:'';
			$info = $this->get_one($id);
			$grade_array = $this->get_grade_array();
			$subject_array = $this->get_subject_array();
			$term_array = $this->get_term_array();
			include template('admin/hschool/files_add');
		}
	}

	/*
	*	删除课时
	*/
	public function del() {
		$is_submit = isset($_REQUEST['is_submit']) ? $_REQUEST['is_submit']:'';
			if(!empty($is_submit)){
			$item_ids = array();
			$item_ids = isset($_REQUEST['item_ids']) ? $_REQUEST['item_ids']:array();
			if($item_ids){
				$update = array(
					'isdelete'=> 1
				);
				$where = ' id IN ('.dimplode($course_ids_array).') ';
				$effects = DB::update('files',$update,$where);
			}
			//DEBUG 接收用户信息 end
			$this->return_json(false,'操作成功');
			echo json_encode($return_array);
		}
	}

	/*
	*	当前动作的首页
	*/
	public function index() {
		global $_G;
		$page_array = array();
		$sqlcount = $sql = $wheresql = $title_keyword = $pageNum = $numPerPage = '';
		$pageNum = isset($_REQUEST['pageNum']) ? $_REQUEST['pageNum'] : 1;
		$numPerPage = isset($_REQUEST['numPerPage']) ? $_REQUEST['numPerPage'] : 6;
		$pagestart = ($pageNum - 1) * $numPerPage;
		$file_name = isset($_REQUEST['file_name']) ? $_REQUEST['file_name'] : '';
		//DEBUG 分层级管理文件 取出子单位建立的文件, 如果是单位管理员则带上本单位编号
		$hschool_dept = load_class('hschool','dept');
		if($hschool_dept){
			$d_id_array=array();
			$d_id_array = $hschool_dept->get_sub_tree($_G['dept_id']);
			if(!empty($d_id_array)){
				$d_id_array=array_keys($hschool_dept->tree2array($d_id_array));
			}
			if($_G['member']['is_dept_master']){
				$d_id_array[] = $_G['dept_id'];
			}
		}
		$wheresql .= " AND f.dept_id IN (".dimplode($d_id_array).")";
		if($file_name){
			$wheresql .= " AND f.file_name LIKE '%".$file_name."%'";
		}
		$sqlcount = "SELECT count(*) from ".DB::table('files')." AS f WHERE f.isdelete=0 ".$wheresql;
		$pagetotal = DB::result_first($sqlcount);
		$sql = "SELECT f.* from ".DB::table('files')." AS f WHERE f.isdelete=0 ".$wheresql." ORDER BY f.file_id DESC LIMIT ".$pagestart.",".$numPerPage;
		$page_array = DB::fetch_all($sql);
		foreach($page_array AS $key => $value){
			$page_array[$key]['grade_name'] = $this->get_grade_name($value['grade_id']);
			$page_array[$key]['subject_name'] = $this->get_subject_name($value['subject_id']);
		}
		//总页数
		$realpages = @ceil($pagetotal / $numPerPage);
		if($realpages <= 0){
			$realpages=1;
		}
		//前一页
		$page_pre = max(1, intval(($pageNum-1)));
		//后一页
		$page_next = min(intval(($pageNum+1)), $realpages);
		//DEBUG 获取文件类型数组
		$type_array = $this->get_type_array();
		include template('admin/hschool/files');
	}

	/*
	* ajax 返回信息
	*/
	public function return_json($error='true',$message="操作失败") {
		$return_array = array(
			"error" => $error,
			"message" => $message
		);
		echo json_ext($return_array);
		die;
	}
	
	/*
	* 年级名称数组
	*/
	function get_grade_array(){
		$return = '';
		$file_path = SITE_ROOT.'./source/function/function_grade_class.php';
		if(file_exists($file_path)){
			include_once $file_path;
			$return = pub_class();
		}
		return $return;
	}
	
	/*
	* 学科名称数组
	*/
	function get_subject_array(){
		$return = '';
		$file_path = SITE_ROOT.'./source/function/function_grade_class.php';
		if(file_exists($file_path)){
			include_once $file_path;
			$return = pub_subject();
		}
		return $return;
	}

	/*
	* 学期名称数组
	*/
	function get_term_array(){
		$return = '';
		$file_path = SITE_ROOT.'./source/function/function_grade_class.php';
		if(file_exists($file_path)){
			include_once $file_path;
			$return = pub_term();
		}
		return $return;
	}

	/*
	* 文件类型名称数组
	*/
	function get_type_array(){
		$return = '';
		$file_path = SITE_ROOT.'./source/function/function_grade_class.php';
		if(file_exists($file_path)){
			include_once $file_path;
			$return = pub_type();
		}
		return $return;
	}
	
	/*
	* 年级名称转换
	*/
	function get_grade_name($grade_id){
		$return = '';
		if($grade_id){
			$grade_array = $this->get_subject_array();
			$return = $grade_array[$grade_id];
		}
		return $return;
	}
	
	/*
	* 学科名称转换
	*/
	function get_subject_name($subject_id){
		$return = '';
		if($subject_id){
			$subject_array = $this->get_subject_array();
			$return = $subject_array[$subject_id];
		}
		return $return;
	}
	
	/*
	* 文件类型名称数组
	*/
	function get_term_name($term_id){
		$return = '';
		$file_path = SITE_ROOT.'./source/function/function_grade_class.php';
		if(file_exists($file_path)){
			$term_array = $this->get_term_array();
			$return = $term_array[$term_id];;
		}
		return $return;
	}
	
	/*
	* 文件类型名称转换
	*/
	function get_type_name($type){
		$return = '';
		if($type){
			include_once SITE_ROOT.'./source/function/function_grade_class.php';
			$pub_type_array = pub_type();
			$return = $pub_type_array[$type];
		}
		return $return;
	}
	
}
?>